package xm.strutsAction;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class ContentFile {
	private static String path = "D:\\test_workspace\\xboard\\WebContent\\BoardData\\";
	private static File file;
	
	//file save
	public static String fileSave(int boardNo, String content, String fileName){
		
		file = new File(path,fileName+".txt");
		
		try {
			PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
			pw.println(content);
			pw.flush();
			pw.close();
		} catch (IOException e) {e.printStackTrace();}
		
		return fileName;
	}
	
	//file reSave
	public static String fileSave(String fileName, String contentResult){
			
			file = new File(path,fileName+".txt");
			
			try {
				PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
				pw.println(contentResult);
				pw.flush();
				pw.close();
			} catch (IOException e) {e.printStackTrace();}
			
			return fileName;
		}
	
	//file read
	public static String fileRead(String fileName, String cmd){
		String result = "";
		String read = "";
		
		file = new File(path,fileName+".txt");
		BufferedReader br = null;
		
		try {
			br = new BufferedReader(new FileReader(file));
			if(cmd.equals("read")){
				while ((read = br.readLine()) != null) {
					result += read+"<br>";
				}
			}else{
				while ((read = br.readLine()) != null) {
					result += read+"\n";
				}
			}
			br.close();	
		} catch (Exception e) {	e.printStackTrace();}
		
		return result;
	}
	
	//file delete
	public static void  fileDel(String fileName) {
		file = new File(path,fileName+".txt");
		
		if(file.exists()){
			file.delete();
			System.out.println("File Delete Success!");
		}else{
			System.out.println("File Delete fail!");
		}
	}
}
